
package foldingborder;

import java.awt.Component;
import java.awt.Container;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

/**
 * @author Jeremy Bergan
 * Creates a Folding section within JTextPane that can be unfolded by clicking on it.
 */

public class FoldingBorder {
	
	JTextPane tp;
	AwesomeTextFold lab;
	/**
	 *  Creates a folding border for the given pane.
	 * @param pane - The JTextPane that is being displayed.
	 */
	public FoldingBorder(JTextPane pane){
		tp = pane;

        try {
    		//Make Collapsible area
    		int start=tp.getSelectionStart();
            int end=tp.getSelectionEnd();
            
            if (start==end) {
                return;
            }
         
            if (start>end) {
                int tmp=start;
                start=end;
                end=tmp;
            }
            
            Document doc = tp.getDocument();
			String text = doc.getText(start, (end - start));
			doc.remove(start, (end - start));
			
			lab= new AwesomeTextFold(text,start);
			
			//Sets the layout to place the folded text	
			tp.insertComponent(lab);
			
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 *  An AwesomeTextFold that holds the folded text and attaches a listener to the tag.
	 * @author Jeremy
	 *
	 */
	public class AwesomeTextFold extends JLabel{
		
		String closed_tag;
		int offset;
		
		/**
		 * Creates an AwesomeTextFold which contains the folded text information
		 * @param t - String to be folded
		 */
	
		public AwesomeTextFold(String t, int arg0){
			offset = arg0;
			int place;
			ImageIcon arrow =  new ImageIcon("foldingborder/arrow.gif", "An Arrow Icon");
			setIcon(arrow); 
			setHorizontalAlignment(JLabel.CENTER);
			try{
				place = t.indexOf('\n');
				setText(t.substring(0, place));
				closed_tag = t.substring(place);
			}
			catch(StringIndexOutOfBoundsException e){
				setText(t);
				closed_tag = "";
			}
			
			//Adds a Mouse Listener that expands the clicked on text.
			addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					//Gets the document of the JTextPane
					Document orig = tp.getDocument();
					int compCount = tp.getComponentCount();
					int spot = 0;
					//Loops Through the Components in the JTextPane and removes the clicked on component as the below approach didn't work
					//tp.remove(tag);		
					//tp.repaint();
					for (int i =0; i < compCount; i++){
						Component check = e.getComponent();
						Component c1 = tp.getComponent(i);
						Component c2 = ((Container)c1).getComponent(0);
						//If this component is equal to the one retrieved then we get remove it from the selection  
						if(check.equals(c2)){
							int start = tp.viewToModel(new Point(c1.getX(),c1.getY()));
							spot = start;
							tp.select(start, start + 1);
							tp.replaceSelection(null);
						}
					}
					try {
						//Places the text back in the document
						orig.insertString(spot , lab.toString(), null);
					} catch (BadLocationException e1) {
						e1.printStackTrace();
					}      
				}      
	        });
			setVisible(true);		
		}
		
		/**
		 *  Returns the text of AwesomeTextFold
		 * @return - The text of both the JLabel elements
		 */	
		/*public String getText(){
			return(closed_tag);
		}*/
	
		public String toString(){
			String s = getText();
			return getText()+closed_tag;
			
		}
	}
	
}
